<?php
/**
 * Object Relational Persistence Mapping Class for table: composite_service_asso.
 *
 * @package com.athena
 * @author  LHV
 */
$loader->requireOnce('ordo/Service.class.php');

class CompositeServiceItem extends ORDataObject {

    /**#@+
     * Fields of table: composite_service_asso mapped to class members
     */
    var $composite_service_item_id = '';
    var $ticket_item_id = '';
    var $service_id = '';
    var $discount = '';
    var $original_price = '';
    var $insurance_price = '';
    var $patient_price ='';

    /**#@-*/

    /**
     * DB Table
     */
    var $_table = 'composite_service_item';

    /**
     * Primary Key
     */
    var $_key = 'composite_service_item_id';
    
    /**
     * Internal Name
     */
    var $_internalName = 'CompositeServiceItem';

    /**
     * Constructor.
     */
    function CompositeServiceItem() {
        parent::ORDataObject();
    }
    
	function addItem($ticketItemId, $serviceId){
		$data = array (
			'ticket_item_id' 	=> $ticketItemId,
			'service_id'		=> $serviceId
			);		
		$this->populateArray($data);		
		$this->persist();			
		return $this->composite_service_item_id;
	}

	public function delete($ticketItemId) {
		$sql = "delete from composite_service_item where ticket_item_id = $ticketItemId";
		$this->_execute($sql);
	}	
	
	public function isItem($ticketItemId, $serviceId){
    	$ds = new Datasource_sql();
        $sql = array(
            'cols'    => "service_id, ticket_item_id",
            'from'    => "composite_service_item",
            'where'   => "ticket_item_id = $ticketItemId AND service_id = $serviceId" 
            );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray('service_id', 'ticket_item_id');
        if (sizeof($result) == 0) return false;
        return true;
	}
	
    function getOptionalServiceByCompositeService($ticketItemId) {
    	$ds = new Datasource_sql();
        $sql = array(
            'cols'    => "s.service_id, s.name, 'yes' as optional",
            'from'    => "composite_service_item i INNER JOIN service s ON (s.service_id = i.service_id)",
            'where'   => "i.ticket_item_id = $ticketItemId" ,
            'orderby' => "s.service_id"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }      
    
    public function getAllServices($ticketItemId){
     	$ds = new Datasource_sql();
        $sql = array(
            'cols'    => "s.service_id, s.name",
            'from'    => "composite_service_item i 
            			INNER JOIN service s ON (s.service_id = i.service_id)",
            'where'   => "i.ticket_item_id = $ticketItemId" ,
            'orderby' => "s.service_id"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();   	
    }
	public function updateItemPrice($ticketItemId, $serviceId, $price){
		$sql = "UPDATE composite_service_item 
				SET original_price = ".$price['original_price'].",
					patient_price = ".$price['patient_price'].",
					discount = ".$price['discount'].",
					insurance_price = ".$price['insurance_price']."
				WHERE ticket_item_id = $ticketItemId AND 
					service_id = $serviceId
				";
		$this->_execute($sql);
	}
    
}
?>